<template>
	<view class="container">
		<view class="status_bar">
			<view style="margin-left: 4%;" @tap="goBack()"><u-icon name="arrow-leftward" size='24' /></view>
			<view style="margin-left: -50upx; font-weight: bold;">隐私安全</view>
			<view></view>
			</u-icon>
		</view>
		<view class="shop-box">
			<view class="con-shop" @tap="toPage('/pages/mine/settings/blackList')">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">黑名单管理</view>
				</view>
				<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
			</view>
			
			
		</view>
		<view class="shop-box">
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">消息提醒</view>
				</view>
				<view style="display: flex;">
					<u-switch v-model="info.message_notify" @change="change" size=20 inactiveValue="0" activeValue="1"
						activeColor="#1ea7dd"></u-switch>
				</view>
			</view>
			<view class="con-shop">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">震动</view>
				</view>
				<view style="display: flex;">
					<!-- 使用 inactiveValue 和 activeValue -->
					<u-switch v-model="info.mesaage_vibrate" @change="changevibrate" size=20 inactiveValue="0"
						activeValue="1" activeColor="#1ea7dd"></u-switch>


				</view>
			</view>



			<view class="con-shop" style="border: none;">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">消息提示音</view>
				</view>
				<view style="display: flex;">
					<u-switch v-model="info.message_notify_music" @change="changemusic" size=20 inactiveValue="0"
						activeValue="1" activeColor="#1ea7dd"></u-switch>
				</view>
			</view>
		</view>

		<view class="shop-box">
			
		
			<view class="con-shop" @tap="handleRecord(type=1)">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">清空所有聊天记录</view>
				</view>
				<view>
					<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
				</view>
			</view>
			<view class="con-shop" @tap="handleRecord(type=2)">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">清除缓存</view>
				</view>
				<view>
					<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
				</view>
			</view>
			<view class="con-shop" style="border: none;" @tap="toPage('/pages/mine/settings/setAboutUs')">
				<view style="display: flex;">
					<view style="margin-top: 10upx;">关于我们</view>
				</view>
				<view>
					<uni-icons type="arrowright" size="18" color="#eaeaea"></uni-icons>
				</view>
			</view>
		</view>

		<view class="bottom-box" @tap="closeAccount()">注销账号</view>
		<view>
			<u-calendar :show="showDate" @confirm="confirm" @close="close" color="#674fa3"></u-calendar>
		</view>
		
	
		<!-- 清除聊天记录/缓存  @tap="recordShow=true"-->
		<u-popup :show="recordShow" @close="recordsClose" mode="center" :round="20">
			<view class="clear-record-box">
				<view class="clear-record-box-title">提示</view>
				<view class="clear-record-box-title1">{{type==1?'确定要清除所有聊天记录':'确定要清除所有缓存记录'}}？</view>
				<view class="clear-record-box-btn">
					<view class="clear-record-box-btn-left" @tap="recordsClose()">取消</view>
					<view class="clear-record-box-btn-right" @tap="recordsConFirm()">确认</view>
				</view>
			</view>
		</u-popup>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: uni.getStorageSync('userId') || '',
				startX: 0, // 触摸开始的X坐标
				endX: 0, // 触摸结束的X坐标
				threshold: 50, // 设置滑动多少距离后触发返回
				user: {},
				nickname: '',
				text1: 'uView UI众多组件覆盖开发过程的各个需求，组件功能丰富，多端兼容。让您快速集成，开箱即用',
				showSex: false,
				info: {
					mesaage_vibrate: "",
					message_notify_music: '',
					message_notify: ''
				},
				columns: [
					[{
						label: '男',
						id: 2021
					}, {
						label: '女',
						id: 804
					}]
				],
				showDate: false,
				value1: false,
				value2: true,
				
				recordShow: false, //聊天记录
				type: 1,
			}
		},
		onLoad() {
			this.getsetting()

		},
		methods: {
			
			// 获取我的设置
			getsetting() {
				console.log('消息提示音', this.info.message_notify_music)
				uni.request({
					url: this.$api.baseUrl + '/api/common/user_setting/get',
					header: {
						'content-type': 'application/json'
					},
					data: {
						user_id: uni.getStorageSync('user_uid')
					},
					method: 'POST',
					success: (res) => {
						this.info = res.data.data;
						this.info.mesaage_vibrate = String(this.info.mesaage_vibrate);
						this.info.message_notify_music = String(this.info.message_notify_music);
						console.log('消息提示音', this.info.message_notify_music);
						uni.setStorageSync('mp3', this.info.message_notify_music)
						uni.setStorageSync('vibrate', this.info.mesaage_vibrate)
						this.info.message_notify = String(this.info.message_notify);
						console.log(res.data.data, "9090");

						
					},
					fail: (res) => {
						console.log(res);
					}
				});
			},

			goBack() {
				uni.navigateBack({
					delta: 1
				});
			},

			changevibrate(e) {
				console.log('震动开关状态：', e); // 打印震动开关状态

				if (e === 0) {
					// uni.vibrateShort({
					// 	success: () => {

					uni.setStorageSync('vibrate', e)
					console.log('短时间震动完成');
					// 	}
					// });
				} else {

					uni.setStorageSync('vibrate', e)
					// 震动禁用
					console.log('震动已关闭');
					uni.vibrateShort({
						success: () => {
							console.log('短时间震动完成');
						}
					});
				}
				// 将震动状态提交给后台
				this.updatePrivacySetting();
			},
			changemusic(e) {
				console.log(e)
				if (e === 0) {
					// 震动启用
					uni.setStorageSync('mp3', e)
				} else {
					// 震动禁用
					uni.setStorageSync('mp3', e)
				}
				// 将震动状态提交给后台
				this.updatePrivacySetting();
			},
			change(e) {
				console.log('change', e);
			},
			handlePickSex() {
				this.showSex = true
			},
			cancelSex() {
				this.showSex = false
			},
			confirmSex(e) {
				console.log('confirm', e)
				this.showSex = false
			},
			confirm(e) {
				console.log(e);
				this.showDate = false
			},
			close() {
				this.showDate = false
			},
			
			
			updatePrivacySetting(range) {
				console.log('this.info.message_notify_music', this.info.message_notify_music);
				uni.request({
					url: this.$api.baseUrl + '/api/common/user_setting/update', // 假设的API接口
					method: 'POST',
					data: {
						user_id: uni.getStorageSync('user_uid'), // 获取用户ID
						mesaage_vibrate: this.info.mesaage_vibrate,
						message_notify: this.info.message_notify,
						message_notify_music: this.info.message_notify_music,
						frend_circle_visibility: range // 朋友圈可见范围
					},
					success: (res) => {
						console.log('隐私设置更新成功', res);
						// 可以根据需要处理响应
					},
					fail: (err) => {
						console.log('隐私设置更新失败', err);
					}
				});
			},

			//清除聊天记录
			recordsOpen() {
				console.log('open');
				this.recordShow = false
			},
			handleRecord(type) {
				this.type = type; // Set the type first
				this.recordShow = true; // Then show the popup
			},


			recordsClose() {
				this.recordShow = false
				// console.log('close');
			},
			recordsConFirm() {
				console.log(this.type)
				if (this.type == 1) {
					let data = `del_flag = 1`;
					// let where = `(relatedId = '${this.info.relatedId}')`
					// 修改表数据 DB.updateTableData(表名, 要修改的列名=修改后列值, 修改条件的列名, 修改条件的列值)
					this.DB.updateTableData("chat" + uni.getStorageSync("user_uid"), data)
						.then(res => {
							console.log(res);
						})
						.catch(error => {
							console.log("修改失败", error);
						});
				} else {
					let that = this;
					let os = plus.os.name;
					if (os == 'Android') {
						let main = plus.android.runtimeMainActivity();
						let sdRoot = main.getCacheDir();
						let files = plus.android.invoke(sdRoot, 'listFiles');
						let len = files.length;
						for (let i = 0; i < len; i++) {
							let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
							plus.io.resolveLocalFileSystemURL(
								filePath,
								function(entry) {
									if (entry.isDirectory) {
										entry.removeRecursively(
											function(entry) {
												//递归删除其下的所有文件及子目录
												uni.showToast({
													title: '缓存清理完成',
													duration: 2000
												});
												that.formatSize(); // 重新计算缓存
											},
											function(e) {
												//console.log(e.message);
											}
										);
									} else {
										entry.remove();
									}
								},
								function(e) {
									//console.log('文件路径读取失败');
								}
							);
						}
					} else {
						// ios
						plus.cache.clear(function() {
							uni.showToast({
								title: '缓存清理完成',
								duration: 2000
							});
							that.formatSize();
						});
					}
				}
				this.recordShow = false
			},
			logout() {
				
				// uni.clearStorageSync()
				uni.MBSocketProvider.closeLocalSocket();
				uni.setStorageSync('loginInfo', {});
				uni.setStorageSync('token', '')
				uni.setStorageSync('user_uid', '')
				uni.setStorageSync('userId', '')
				uni.setStorageSync('user_level', '')
				uni.setStorageSync('nickname', '')
				uni.setStorageSync('user_avatar_file_name', '')
				uni.setStorageSync('loginInfoArr', []);
				this.toPage('/pages/login')
			},
			showEdit() {
				this.groupName = this.group.name
				this.$refs.edit.open()
			},
			cancelEdit() {
				this.$refs.edit.close()
			},
			editAction() {
				if (this.$validate.isNull(this.groupName)) return

				uni.request({
					method: 'POST',
					url: this.$api.updateGroupName,
					data: {
						groupId: this.groupId,
						userId: this.userId,
						name: this.groupName
					},
					header: this.$util.getHeader(),
					success: res => {
						uni.hideLoading()
						this.$util.toast(res.data.msg)
						if (!res.data.code) {
							this.$refs.edit.close()
							this.group.name = this.groupName
							uni.setStorageSync('groupName', this.groupName)
						}
					},
					fail: res => {
						uni.hideLoading()
						this.$util.toast('保存失败')
					}
				})
			},
			// 上传头像
			uploadFace() {
				uni.chooseImage({
					count: 1,
					success: chooseRes => {
						const tempFilePaths = chooseRes.tempFilePaths

						this.$util.loading('上传中')
						tempFilePaths.forEach(temp => {
							uni.uploadFile({
								url: this.$api.uploadUserFace,
								filePath: temp,
								formData: {
									groupId: this.groupId,
									userId: this.userId
								},
								name: 'file',
								header: this.$util.getHeader(),
								success: uploadRes => {
									uni.hideLoading()
									if (uploadRes.data.code) return
									let data = JSON.parse(uploadRes.data)
									this.user.face = this.$api.staticPath + data.data
									uni.setStorageSync('userFace', this.user.face)
								}
							})
						})
					}
				})
			},
			// 注销
			closeAccount() {
				uni.showModal({
					title: '',
					content: '注销后无法恢复，确定注销？',
					cancelColor: '#000',
					confirmColor: '#40AFFE',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							this.closeAccount1();
						} else if (res.cancel) {}
					}
				});
			},
			closeAccount1() {

				const newDataObj = {
					"uid": uni.getStorageSync('user_uid'),
					"deviceInfo": "0", // 手机硬件信息等，可自定义内容
					"osType": "0",
				};
				const newDataJsonStr = JSON.stringify(newDataObj)
				const requestData = {
					// "processorId": '2',
					"jobDispatchId": '2',
					// "actionId": '2',
					"newData": newDataJsonStr
				};

				const requestDataJsonStr = JSON.stringify(requestData);
				uni.request({
					url: 'http://chat.zwsmcn.com:8090/rest_post',
					header: {
						'content-type': 'application/json'
					},
					data: requestDataJsonStr,
					method: 'POST',
					success: res => {
						console.log(res);
						uni.removeStorageSync('user_uid');
						uni.removeStorageSync('user_token');
						uni.removeStorageSync('user_info');

						uni.setStorageSync('loginInfo', {});
						uni.setStorageSync('token', '')
						uni.setStorageSync('user_uid', '')
						uni.setStorageSync('userId', '')
						uni.setStorageSync('user_level', '')
						uni.setStorageSync('nickname', '')
						uni.setStorageSync('user_avatar_file_name', '')
						uni.setStorageSync('loginInfoArr', []);
						uni.IMSDK.disconnectSocket();
						uni.MBSocketProvider.closeLocalSocket();
						uni.reLaunch({
							url: '/pages/login' // 登录页面路径
						});

					},
					fail: res => {
						console.log(res);
						uni.showToast({
							title: '网络错误，请稍后再试',
							icon: 'none'
						});
					}
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 100%;
		height: 100%;
	}

	.status_bar {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 50upx;
	}

	.status_bar,
	.head {
		background-color: #fff;
		height: 160upx;

		.face {
			width: 160upx;
			height: 160upx;
			border-radius: 10upx;
		}

		.info-name {
			font-size: 40upx;
			font-weight: bold;
		}

		.info-num {
			// width: 56upx;
			padding: 2upx 30upx;
			background: #f0f4f7;
			border-radius: 30upx;
			margin-top: 21upx;
		}

		.copybtns {
			color: #4457a4;
			margin-top: 21upx;
		}
	}

	.items {
		height: 100upx;
		padding: 0 30upx;
		background-color: #FFF;
		border-bottom: 2upx solid #F2F2F2;

		.label {
			color: #666;
			font-size: 32upx;
		}

		.value {
			color: #999;
			font-size: 30upx;
		}

		.face {
			width: 100upx;
			height: 100upx;
			border-radius: 10upx;
		}
	}

	.main-btn {
		color: red;
		height: 100upx;
		padding: 0 30upx;
		margin-top: 100upx;
		background-color: #FFF;
	}

	.shop-box {
		width: 92%;
		margin-left: 4%;
		background-color: #fff;
		margin-top: 20upx;
		border-radius: 20upx;
		padding: 10upx 30upx;
		box-shadow: 0 0 40upx #d6e7f1; // 左
		box-shadow: 0 0 40upx #d6e7f1; // 右
		color: #2e2e2e;
		font-weight: 300;

		image {
			width: 70upx;
			height: 70upx;
			padding: 10upx 28upx 0upx 10upx;

		}
	}

	.con-shop {
		height: 100upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-bottom: 1upx solid #f7f7f7;

		span {
			padding-right: 10upx;
			color: #b5b5b5;
			font-size: 28upx;
		}

		.image-code {
			width: 42upx;
			height: 42upx;
			padding: 10upx 28upx 0upx 10upx;
		}
	}

	.bottom-box {
		width: 92%;
		margin-left: 4%;
		background-color: #fff;
		margin-top: 18upx;
		border-radius: 20upx;
		padding: 30upx 30upx;
		box-shadow: 0 0 40upx #d6e7f1; // 左
		box-shadow: 0 0 40upx #d6e7f1; // 右
		margin-bottom: 20upx;
		color: #d84e65;
		text-align: center;
	}

	.clear-record-box {
		border-radius: 30upx;
		padding: 30upx;

		.clear-record-box-title {
			padding: 10upx 20upx;
			font-size: 40upx;
		}

		.clear-record-box-title1 {
			padding: 20upx 0 30upx 20upx;
			font-size: 30upx;
			font-weight: 300;
		}

		.clear-record-box-btn {
			display: flex;
			// padding: 30upx 0 20upx 0;
			margin-left: 150upx;
		}

		.clear-record-box-btn-left {
			padding: 10upx 30upx;
		}

		.clear-record-box-btn-right {
			background: #000;
			color: #fff;
			padding: 10upx 40upx;
			margin-left: 30upx;
		}
	}
</style>